import socket

def remote_local_execution_switch_button ( ) :

    global xrn_main_window_global
    slayout_local = xrn_main_window_global.jlayout_str

    selection = get_from_layout ( slayout_local, "remote_local_execution_switch_id" )["fields"][2]["selection"]

    obj = get_from_layout ( slayout_local, "remote_execution_ip_address" )
    if selection == "local" :
        obj["permission"] = "read_only"
    else :
        obj["permission"] = "read_write"
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "remote_execution_tcp_address" )
    if selection == "local" :
        obj["permission"] = "read_only"
    else :
        obj["permission"] = "read_write"
    slayout_local = set_to_layout( slayout_local, obj )

    obj = get_from_layout ( slayout_local, "ping_remote_server_id" )
    if selection == "local" :
        obj["permission"] = "read_only"
    else :
        obj["permission"] = "read_write"
    slayout_local = set_to_layout( slayout_local, obj )

    render_layout(  slayout_local )


def ping_remote_server_button () :

    global xrn_main_window_global
    slayout_local = xrn_main_window_global.jlayout_str

    tcp_address = get_from_layout ( slayout_local, "remote_execution_tcp_address" )["fields"][2]["display"]

    try :
        tcp_address_int = int(tcp_address)
    except :
        tcp_address_int = -1


    if ( tcp_address_int > 65535 ) :
        print("tcp address " + tcp_address + " not valid") 
    elif ( tcp_address_int < 0 ) :
        print("tcp address " + tcp_address + " not valid") 
    else :
        print("tcp address " + tcp_address + " valid") 

    ip_address = get_from_layout ( slayout_local, "remote_execution_ip_address" )["fields"][2]["display"]

    try:
        socket.inet_aton(ip_address)
        print("ip address " + ip_address + " valid")

    except socket.error:  # Specific exception for invalid IP address
        print("ip address " + ip_address + " not valid") 


